(function(){
         /*只有在stage叫呼update()或tick()方法的時候，canvas才會重畫*/
    var stage = new createjs.Stage("game");
    createjs.Ticker.setFPS(30);
    createjs.Ticker.addEventListener("tick",stage);
    var gameview = new createjs.Container();
    stage.addChild(gameview);
    gameview.x = 30;
    gameview.y = 30;

    //注意水平方向才是x   竖直方向为y 这由元素在页面绘制而定
    var circleArr = new Array();
    var currentCat ;
    //创建动画人物
    var  spritesheet = new createjs.SpriteSheet({
         'images':['img/people.png'],
         'frames':{"height":96,"count":10,"width":75}
    });
    var cat = new createjs.Sprite(spritesheet);
    var catW = 38;
    var catH = 51;
    cat.play();
    gameview.addChild(cat);
    //移动方向
    //1 可走   2围栏  3 猫的位置
    var height = 9,width = 9;
    var dxy = [[-1,0],[1,0],[-1,-1],[0,-1],[-1,1],[0,1]];
    var ESCAPED = -2,CAPTURED = -1;
    var vis = [];
    var clickx,clicky;
    for(var i = 0;i<height;i++){
        vis[i] = [];
        for(var j=0;j<width;j++)
                vis[i][j] = 0;
     }

    function point(){
        var x;
        var y;
        var step;
    }

    function bfs(x,y){
         if(circleArr[x][y].getColorType() == 2)
             return -1;
         var head = 0,rear =1;
         var points = [];
         for(var i = 0;i<height;i++)
                for(var j=0;j<width;j++)
                      vis[i][j] = 0;
         vis[clickx][clicky] = 1;
         p = new point();
         p.x = x;p.y = y;p.step = 1;
         points[0] = p;
         vis[x][y] = 1;
         while(head<rear){
            var p = points[head++];
            if(p.x==0 ||p.x==width-1 || p.y==0||p.y==height-1){
               // console.log("("+p.x+","+p.y+")");
                return p.step;
            }
            for(var i = 0;i<6;i++){
                var xx =  p.x+dxy[i][0];
                var yy =  p.y+dxy[i][1];
                if(dxy[i][1] != 0)
                     xx += p.y%2;     ///奇偶行不一样
                var step = p.step;
                if(xx>=0&&xx<width&&yy>=0&&yy<height){
                    if(vis[xx][yy] == 1)  continue;
                    if(circleArr[xx][yy].getColorType()==2)  continue;
                    vis[xx][yy] = 1;
                    var p = new point();
                    p.x = xx;p.y = yy;
                    p.step = step+1;
                    points[rear++] = p;
                }
            }
         }
         return -1;
    }

    var dis = [-1,-1,-1,-1,-1,-1];

    function dfs(x,y,step,index){
        if(circleArr[x][y].getColorType() == 2){
            dis[index] = -1;
            return ;
        }
            
        if(x==0||x==width-1 || y==0|| y== height-1)
        {
            if(dis[index] == -1 ) dis[index] = step;
            else if(step<dis[index]) dis[index] = step;
            return;
        }
        for(var i=0;i<6;i++){
                var xx =  x+dxy[i][0];
                var yy =  y+dxy[i][1];
                if(dxy[i][1] != 0)
                     xx += y%2;     ///奇偶行不一样
                if(xx>=0&&xx<width&&yy>=0&&yy<height){
                    if(vis[xx][yy] == 1 || vis[xx][yy] == -1)  continue;
                    if(circleArr[xx][yy].getColorType()!=1)  continue;
                    vis[xx][yy] = 1;
                    dfs(xx,yy,step+1,index);
                }
        }
        vis[x][y] = -1;
    }

    function getDir(cat){
         var x = cat.indexX;
         var y = cat.indexY;
         if(x==0 || x==8 ||  y == 0 || y== 8){
            return ESCAPED;
         }
         var s ="";
         for(var i=0;i<6;i++){
             var xx = x+dxy[i][0];
             var yy = y+dxy[i][1];
            // dis[i] = bfs(xx,yy); 
             /*属于dfs*/
             if(dxy[i][1] != 0)
                    xx += y%2;     ///奇偶行不一样
             for(var k = 0;k<height;k++)
                for(var j=0;j<width;j++)
                      vis[k][j] = 0;
             vis[clickx][clicky] = 1;
             dfs(xx,yy,1,i);
             /*属于dfs*/
                        
            // s +=dis[i].toString();
         }
         var minLen = 100;
         var minI = -1;
         var flag = 0;
         for(var i=0;i<6;i++){
            if(dis[i]!=-1){
                if(dis[i]<minLen){
                    minLen = dis[i];
                    minI = i;
                    flag = 1;
                }
            }
         }
         if(flag == 0)  return CAPTURED;
         return minI;
    }

    function move(){
         var y = currentCat.indexY;
         var x = currentCat.indexX;
         var offset = y%2;
         var dir = getDir(currentCat);
         if(dir == ESCAPED){
            alert("逃跑了！");
            return;
         }else if(dir == CAPTURED){
            alert("恭喜您抓住了！");
            return;
         }else{
            var x0 = x+dxy[dir][0];
            var y0 = y+dxy[dir][1];
            if(dxy[dir][1] != 0)
                 x0 += y%2;
            var nextcat = circleArr[x0][y0]; 
            currentCat.setColorType(1);
            currentCat.visible  = true;
            cat.x =  nextcat.x-catW ;
            cat.y =  nextcat.y-catH ;
            nextcat.setColorType(3);
            currentCat = nextcat;
            currentCat.visible  = false;
         }
    }

    function clicked(event){
        var el = event.target;
        if(el.getColorType()== 1){
            clickx = el.indexX;
            clicky = el.indexY;
            el.setColorType(2);
                        move();
        }
                //点击猫或者障碍物不执行操作      
    }

    function addCircle(rows,columns){
            for(var x = 0;x<rows;x++)
            {
                circleArr[x] = new Array();
                for(var y = 0;y<columns;y++){
                    var c = new Circle();
                    gameview.addChild(c);  ////每次创建逗得加入到容器中
                    c.indexX = x;
                    c.indexY = y;  
                    c.x = y%2?x*65+25:x*65;   /////注意水平方向才是x   竖直方向为y 这由元素在页面绘制而定
                    c.y = y*65;
                    if(x==4 && y== 4){
                        c.setColorType(3);
                        currentCat = c;
                        cat.x = c.x-catW ;
                        cat.y = c.y-catH ;
                        c.visible = false;
                       
                    }
                    else if(Math.random()<0.14){
                         c.setColorType(2);
                    }
                    circleArr[x][y] = c ;
                    c.addEventListener("click",clicked);
                }
            }
    }
    addCircle(height,width);
    stage.update();
    
})();




